#include "include/trip.h"
// 解决byte命名冲突问题
#define byte win_byte_override
#include "include/mysql.h"
#undef byte

// 全局变量
extern MysqlUser* mysql;

// 构造函数和析构函数
Trip::Trip() :planeId(0), price(0), tripId(0){}
Trip::~Trip() {}


// CRUD
bool Trip::AddTrip(unsigned int tripId, unsigned int planeId,string takeOffCity, string fallCity, double price,string takeOffTime, string fallTime) {
	// 对时间进行处理
	long long tTime = TurnTimeStyle(takeOffTime);
	long long fTime = TurnTimeStyle(fallTime);

	// SQL语句
	std::stringstream sqlStream;
	sqlStream << "INSERT INTO trip (tripId, planeId, takeOffCity,fallCity,price,takeOffTime,fallTime) VALUES ("
		<< tripId <<","<< planeId << ",'" << takeOffCity <<"','" << fallCity<<"'," << price <<","<< to_string(tTime) <<","<< to_string(fTime) <<")";
	std::string sqlStr = sqlStream.str();

	if (!mysql->TransactionExecuteQuery(sqlStr)) {
		return false;
	}
	return true;

}
bool Trip::DeleteTrip(unsigned int tripId) {
	std::stringstream sqlStream;
	sqlStream << "delete from trip where tripId = " << tripId;
	std::string sqlStr = sqlStream.str();
	if (!mysql->TransactionExecuteQuery(sqlStr)) {
		return false;
	}
	return true;
}
bool Trip::ModifyTrip(unsigned int tripId, map<string, string> entry) {
	map<string, string>::iterator it = entry.begin();
	// 日期处理
	string item = to_string(TurnTimeStyle(it->second));
	if (item == "-1") {
		item = it->second;
	}
	//SQL 语句
	std::stringstream sqlStream;
	sqlStream << "update trip set " << it->first << " = '" << item << "' where tripId = " << tripId;
	std::string sqlStr = sqlStream.str();
	if (!mysql->TransactionExecuteQuery(sqlStr)) {
		return false;
	}
	return true;
}
Trip Trip::SearchTrip(unsigned int tripId) {
	std::stringstream sqlStream;
	sqlStream << "select * from trip where tripId = " << tripId;
	std::string sqlStr = sqlStream.str();

	vector<vector<string>> result = mysql->select(sqlStr);

	this->setTripId(stoi(result[0][0]));
	this->setPlaneId(stoi(result[0][1]));
	this->setTakeOffCity(result[0][2]);
	this->setFallCity(result[0][3]);
	this->setPrice(stod(result[0][4]));
	this->setTakeOffTime(ParseTimeStyle(stol(result[0][5])));
	this->setFallTime(ParseTimeStyle(stol(result[0][6])));

	return *this;
}

// 查询所有航班
vector<Trip> Trip::SearchAllTrip() {
	vector<Trip> trips;
	string sqlStr = "select * from trip";
	vector<vector<string>> results = mysql->select(sqlStr);

	// 对结果进行装载
	vector<vector<string>>::iterator it = results.begin();
	for (it; it != results.end(); it++) {
		vector<string>::iterator it2 = (*it).begin();

		Trip p;
		p.setTripId(stoi(it2[0]));
		p.setPlaneId(stoi(it2[1]));
		p.setTakeOffCity(it2[2]);
		p.setFallCity(it2[3]);
		p.setPrice(stod(it2[4]));
		p.setTakeOffTime(ParseTimeStyle(stol(it2[5])));
		p.setFallTime(ParseTimeStyle(stol(it2[6])));

		trips.push_back(p);
	}
	return trips;
}


// 成员变量访问和修改函数的实现
unsigned int Trip::getTripId() const {
	return tripId;
}

void Trip::setTripId(unsigned int id) {
	tripId = id;
}

unsigned int Trip::getPlaneId() const {
	return planeId;
}

void Trip::setPlaneId(unsigned int id) {
	planeId = id;
}

string Trip::getTakeOffCity() const {
	return takeOffCity;
}

void Trip::setTakeOffCity(const string& city) {
	takeOffCity = city;
}

string Trip::getFallCity() const {
	return fallCity;
}

void Trip::setFallCity(const string& city) {
	fallCity = city;
}

double Trip::getPrice() const {
	return price;
}

void Trip::setPrice(double p) {
	price = p;
}

string Trip::getTakeOffTime() const {
	return takeOffTime;
}

void Trip::setTakeOffTime(const string& time) {
	takeOffTime = time;
}

string Trip::getFallTime() const {
	return fallTime;
}

void Trip::setFallTime(const string& time) {
	fallTime = time;
}